<template>
  <div>
    <div class="shopCart">
      <div class="cart-icon">
        <ul>
          <li>
            <a href="#">
              <div class="contact">
                <div>
                  <i class="bi bi-headset"></i>
                </div>
                <span>联系客服</span>
              </div>
            </a>
          </li>
          <li>
            <a href="#">
              <div class="collect" @click="toggleCollect">
                <div>
                  <i v-if="!isHeart" class="bi bi-heart"></i>
                  <i v-if="isHeart" class="bi bi-heart-fill light"></i>
                </div>
                <span>收藏</span>
              </div>
            </a>
          </li>
          <li>
            <a href="#">
              <div class="cart">
                <div>
                  <i class="bi bi-cart"></i>
                </div>
                <span>购物车</span>
              </div>
            </a>
          </li>
        </ul>
      </div>
      <div class="btn-cart">
        <span @click="addTo">加入购物车</span>
      </div>
    </div>
    <div class="mask" v-if="isMask">
      <div class="cart-number" ref="mask">
        <div class="cart_detail">
          <div class="detail_img">
            <img :src="path" alt="" />
          </div>
          <div class="cart_con">
            <div class="title">
              <span>{{ title }}</span>
            </div>
            <div class="price">
              <div class="rmb">
                <span>￥</span>
                <span>{{ price }}</span>
              </div>
              <div class="sales">
                <span>库存:{{ sale }}</span>
              </div>
            </div>
          </div>
          <div class="cart_icon" @click="removeTo">
            <i class="bi bi-plus-lg"></i>
          </div>
        </div>
        <div class="cart_count">
          <span>数量</span>
        </div>
        <div class="cart_total">
          <div class="dec" @click="decFn">-</div>
          <input type="text" v-model="num" />
          <div class="add" @click="addFn">+</div>
        </div>
        <div class="cart_comfire" @click="addShop">
          <span>确认</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";

export default {
  data() {
    return {
      isHeart: false,
      isMask: false,
      detailObj: {},
      title: "",
      path: "",
      price: "",
      sale: "",
      num: 1,
      productId : "",
      uniqueId : "",
    };
  },
  computed: {
    ...mapState(["commod"]),
  },
  methods: {
    init() {
      let data = this.$store.state.commod.commodList[0].storeInfo;
      let res = this.$store.state.commod.commodList[0].productValue;
      // console.log(res,888);
      // console.log(data,222);
      let obj = {};
      let collectObj = {};
      obj.id = data.id;
      obj.num = 1;
      obj.path = data.image;
      obj.price = data.price;
      obj.title = data.store_name;
      obj.check = data.userCollect;
      obj.sale = data.sales;

      collectObj.product = res[38].product_id;
      collectObj.unique = res[38].unique;

      this.uniqueId = collectObj.unique
      this.productId = collectObj.product


      this.detailObj = obj;
      // console.log(this.uniqueId,222);
    },
    addTo() {
      this.init();
      this.isMask = true;
      this.title = this.detailObj["title"];
      this.path = this.detailObj["path"];
      this.price = this.detailObj["price"];
      this.sale = this.detailObj["sale"];
    },
    removeTo() {
      this.isMask = false;
    },
    decFn() {
      this.num--;
      if (this.num < 2) {
        this.num = 1;
      }
    },
    addFn() {
      this.num++;
    },
    toggleCollect() {
      this.init();
      // console.log(this.detailObj,111);
      //取反
      this.isHeart = !this.isHeart;
      // if(this.collectList.length == 0){
      //   this.collectList.push(this.detailObj);
      //    localStorage.setItem("collectList", JSON.stringify(this.collectList));
      // }
      let array = JSON.parse(localStorage.getItem("collectList") || "[]");
      if (this.isHeart == true) {
        let temp = -1;
        array.forEach((item, index) => {
          if (item.id == this.detailObj["id"]) {
            temp = index;
          }
        });
        if (temp != -1) {
          array.splice(temp, 1);
        }
      } else {
        let index = array.findTndex((item) => {
          return (item.id = this.detailObj["id"]);
        });
        array.splice(index, 1);
      }
      array.unshift(this.detailObj);
      localStorage.setItem("collectList", JSON.stringify(array));
    },
    addShop() {
      let token = sessionStorage.getItem("token");
      console.log(this.productId,111);
      console.log(this.uniqueId,222);
      axios({
        method: "POST",
        url: `http://47.115.51.185/api/cart/add`,
        headers: {
          "Authori-zation":"Bearer "+ token,
        },
        data:{
          cartNum : this.num,
          new : 0,
          uniqueId : this.uniqueId,
          productId : this.productId,
        }
      })
      .then((res) => {
        console.log(res);
        if(res.data.status == 200){
          this.$toast("加入购物车成功！");
          this.isMask = false;
        }
      });
    },
  },
  created() {
    this.init();
  },
};
</script>

<style lang="less">
.shopCart {
  width: 100%;
  height: 50px;
  bottom: 0;
  left: 0;
  position: fixed;
  background: #fff;
  box-sizing: border-box;
  border-top: 2px solid #f0f0f0;
}
.cart-icon {
  width: 215px;
  height: 50px;
  float: left;
  ul {
    display: flex;
    li {
      width: 100%;
      height: 50px;
      text-align: center;
      line-height: 20px;
      padding-top: 8px;
      box-sizing: border-box;
      i {
        font-size: 20px;
      }
      span {
        font-size: 14px;
      }
    }
  }
}
.btn-cart {
  width: 150px;
  height: 38px;
  line-height: 38px;
  float: left;
  text-align: center;
  border-radius: 20px;
  color: #fff;
  background: linear-gradient(to right, #7028e4, #ee609c);
  margin-top: 8px;
  font-size: 16px;
}
.light {
  color: #f00;
}

.mask {
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  position: fixed;
  background: rgba(0, 0, 0, 0.6);
}
.cart-number {
  width: 100%;
  height: 300px;
  background: #fff;
  position: absolute;
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
  bottom: 0;
  transform: translateY(0px);
}
.cart_detail {
  width: 100%;
  height: 150px;
  > div {
    float: left;
  }
  .detail_img {
    width: 110px;
    height: 110px;
    margin-top: 15px;
    margin-left: 10px;
    img {
      width: 100%;
      height: 100%;
      display: block;
    }
  }
  .cart_con {
    width: 210px;
    height: 110px;
    margin-top: 15px;
    margin-left: 5px;
    .title {
      width: 100%;
      height: 80px;
      font-size: 20px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .price {
      width: 100%;
      height: 30px;
      line-height: 30px;
      display: flex;
      .rmb {
        width: 120px;
        height: 30px;
        color: #9200f9;
        span {
          &:first-child {
            font-size: 14px;
          }
          &:last-child {
            font-size: 24px;
          }
        }
      }
      .sales {
        flex-grow: 1;
        font-size: 16px;
        line-height: 40px;
        color: #999999;
      }
    }
  }
  .cart_icon {
    width: 25px;
    height: 25px;
    color: #999999;
    transform: rotate(45deg);
    margin-left: 6px;
    margin-top: 5px;
    font-size: 28px;
  }
}

.cart_count {
  width: 100%;
  height: 45px;
  color: #999;
  font-size: 22px;
  padding-left: 10px;
}
.cart_total {
  width: 200px;
  height: 30px;
  padding-left: 10px;
  display: flex;
  > div {
    width: 50px;
    height: 25px;
    line-height: 25px;
    text-align: center;
    font-size: 26px;
  }
  input {
    width: 70px;
    height: 25px;
    background: #f0f0f0;
    outline: none;
    border: none;
    font-size: 18px;
    border-radius: 20px;
    text-align: center;
  }
}
.cart_comfire {
  width: 300px;
  height: 40px;
  line-height: 40px;
  border-radius: 20px;
  margin: 28px auto 0px;
  text-align: center;
  color: #fff;
  font-size: 22px;
  background: linear-gradient(to left, #8f00fc, #b303e3);
}
</style>